local skynet = require "skynet"
local cluster = require "skynet.cluster"
require "skynet.manager"
local json = require "cjson"
json.encode_sparse_array(true,1,1)

local filelog = require "filelog"
local configdao = require "configdao"
local redisdao = require "redisdao"
local mysqldao = require "mysqldao"
local timetool = require "timetool"

skynet.start(function()
	skynet.error("gmnode start")
	skynet.newservice("nodelog")
	local confsharesvr = skynet.newservice("confshare")
	skynet.call(confsharesvr, "lua", "start")

	if not skynet.getenv "daemon" then
		local console = skynet.newservice("console")
	end

	skynet.name(".router", skynet.uniqueservice("router"))	
	skynet.call(".router", "lua", "genfreeridpool")

	skynet.name(".gmmqredis", skynet.uniqueservice("redisdb"))	
	skynet.call(".gmmqredis", "lua", "init", configdao.get_cfgsvrs_key("gmmqredis"))

	
	skynet.name(".nodeuuid", skynet.uniqueservice("nodeuuid") )
	-- print("line46", skynet.call(".nodeuuid", "lua", "getuuid"))

	skynet.name(".gamedatalog", skynet.newservice("datalog"))
	skynet.call(".gamedatalog", "lua", "init", "gamedata")		

	skynet.name(".rank", skynet.uniqueservice("rank"))	
	skynet.call(".rank", "lua", "get_rank_from_redis")
	
    skynet.name(".clientver", skynet.uniqueservice("clientver"))	
	skynet.call(".clientver", "lua", "get_clientver_from_redis")

    skynet.name(".massmail", skynet.uniqueservice("massmail"))	
	skynet.call(".massmail", "lua", "get_massmail_from_redis")

    skynet.name(".processcmd", skynet.uniqueservice("processcmd"))	
	
	cluster.open("gmnode")

-----------------------------------------------------------------------------
	--                 下面都是测试代码
	-----------------------------------------------------------------------------

	-- local cmd = {msghead={msgname="change_money"}, msgbody={rid=100412, itemvalues = {fangka=3}}}
	-- local cmd = {msghead={msgname="charge_diamond"}, msgbody={rid=100412, value = 5001}}
	-- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	-- local cmd = {msghead={msgname="charge_fangka"}, msgbody={rid=7061, value = 100}}
	-- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	--[[
	local rid_values = {}
	rid_values[7061] = {beginvalue=1, value=-199, endvalue=3, rolename="nameabc", had_half=true}
	rid_values[35740] = {beginvalue=1, value=199, endvalue=3, rolename="name甲乙丙", had_half=true}
	local comment = {}
	comment.table_uuid = 'psznode_123456_1_167654321'
	comment.table_tradeid = 1234567890
	-- local cmd = {msghead={msgname="game_half_diamond"}, 
	-- local cmd = {msghead={msgname="game_balance_diamond"}, 
	-- local cmd = {msghead={msgname="game_balance_fangka"}, 
	local cmd = {msghead={msgname="game_sub_fuwufei"}, 
				msgbody={rid_values =rid_values, comment=comment}
		}
	redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))

	-- ]]
	--[[跑马灯
	local cmd = {msghead={msgname="addhrlmsg"}, msgbody={msg={level=1, repeatnum=4, content="this horse race lamp msg from GM"}}}
	redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	local cmd = {msghead={msgname="addhrlmsg"}, msgbody={msg={level=2, repeatnum=4, content="this horse race lamp msg from GM"}}}
	redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	local cmd = {msghead={msgname="addhrlmsg"}, msgbody={msg={level=3, repeatnum=4, content="this horse race lamp msg from GM"}}}
	redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	local cmd = {msghead={msgname="addhrlmsg"}, msgbody={msg={level=4, repeatnum=4, content="this horse race lamp msg from GM"}}}
	]]
	
	-- local cmd = {msghead={msgname="kickrole"}, msgbody={rid=7061}}
	-- local cmd = {msghead={msgname="newmail"}, msgbody={rid=7061, fields={subject="abcsubject", content="some content", attach_json=json.encode({diamond=123})}}}

	-- local cmd = {msghead={msgname="getridbyuid"}, msgbody={uid=22344}}
	-- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))

	-- local cmd = {msghead={msgname="renew_rank"}, msgbody={linenum=50}}
	-- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	
	 -- local cmd = {msghead={msgname="get_online_state"}, msgbody={}}
	 -- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))

	local a = {}
	a[1] = {subject="@#$&_mj9902", content="", pic="", target=""}
	a[2] = {subject="", content="", pic="https://d.chinalucky.net/ad_longhudou.png", target="lhd"}
	a[3] = {subject="", content="", pic="https://d.chinalucky.net/ad_hongbao.png", target="hbsl"}
	local cmd = {msghead={msgname="set_system_ad"}, msgbody={addata=a}}
	redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	
	-- local cmd = {msghead={msgname="newmail"}, msgbody={rid=7061, fields={subject="abcsubject", content="some content", attach_json=json.encode({diamond=123})}}}
	-- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
	-- local cmd = {msghead={msgname="newmail"}, msgbody={rid=7061, fields={subject="abcsubject", content="some content", attach_json=json.encode({diamond=123})}}}
	-- redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd))
end)
